--This is part of the door module
local meta = FindMetaTable( "Entity" );
 /*---------------------------------------------------------
   Name: IsOwnable()
   Desc: Are we a door?
---------------------------------------------------------*/
function meta:IsDoor()

	local class = self:GetClass();
	
	if (class == "prop_dynamic") then
		if (self.GetKeyValues) then
			if (self:GetSolid() == 6) then
				if (tostring(self:GetKeyValues().MinAnimTime) == "1") then
					if (tostring(self:GetKeyValues().MaxAnimTime) == "5") then
						return true
					end
				end
			end
		end
	end
	
	if( class == "func_door" or
		class == "func_door_rotating" or
		class == "prop_door_rotating") then
		
		return true;
		
	end
	
	return false;

end

/*---------------------------------------------------------
   Name: GetStatus()
   Desc: Gets the status of the door
 --  0/nil = unowned
 --  1 = owned
 --  2 = unownable
---------------------------------------------------------*/
function meta:GetStatus()

	if (self:GetNWInt( "status" ))then
		return self:GetNWInt( "status" );
	else
		return 0;
	end

end



local ToolsBlackList = {
	"colour",
	"material",
	"paint",
	"duplicator",
	"eyeposer",
	"faceposer",
	"remover",
	"colour",
	"material",
	"paint",
	"hoverball",
	"emitter",
	"elastic",
	"hydraulic",
	"muscle",
	"nail",
	"ballsocket",
	"ballsocket_adv",
	"pulley",
	"rope",
	"slider",
	"weld",
	"winch",
	"balloon",
	"button",
	"duplicator",
	"dynamite",
	"keepupright",
	"lamp",
	"nocollide",
	"thruster",
	"turret",
	"wheel",
	"eyeposer",
	"faceposer",
	"statue",
	"weld_ez",
	"axis",
}

function meta:DisallowMoving( bool )
	self.NoMoving = bool
end

function meta:DisallowDeleting( bool )
	self.NoDeleting = bool
end

function CanTheyUseThatTool( ply, tr, toolmode, second )

	-- In the case of the nail gun, let's check the entity they're nailing TO first.
	if toolmode == "nail" and not second then
		local tr2 = {}
		tr2.start = tr.HitPos
		tr2.endpos = tr.HitPos + ply:GetAimVector() * 16
		tr2.filter = { ply, tr.Entity }
		local trace = util.TraceLine( tr2 )

		if trace.Entity and trace.Entity:IsValid() and not trace.Entity:IsPlayer() then
			local ret = CanTheyUseThatTool( ply, trace, toolmode, true )
			if ret ~= nil then
				return ret
			end
		end
	end

	-- In the case of the remover, we have to make sure they're not trying to right click remove one of no delete ents
	if toolmode == "remover" and ply:KeyDown( IN_ATTACK2 ) and not ply:KeyDownLast( IN_ATTACK2 ) then
		local ConstrainedEntities = constraint.GetAllConstrainedEntities( tr.Entity )
		if ConstrainedEntities then -- If we have anything to worry about
			-- Loop through all the entities in the system
			for _, ent in pairs( ConstrainedEntities ) do
				if ent.NoDeleting then
					Notify( ply, 1, 4, "This entity is constrained to non-deletable entities!" )
					return false
				end
			end
		end
	end

	if tr.Entity.NoDeleting then
		if table.HasValue( ToolsBlackList, toolmode ) then
			return false
		end
	end

end

function AllowPickupWithPhysGun( ply, ent )
	if ent.NoMoving then return false end
end

function AllowPickupWithGravGun( ply, ent )
	if ent.NoMoving then return false end
end

hook.Add( "PhysgunPickup", "AllowPickupWithPhysGun", AllowPickupWithPhysGun )
hook.Add( "GravGunPickupAllowed", "AllowPickupWithGravGun", AllowPickupWithGravGun )
hook.Add( "CanTool", "CanTheyUseThatTool", CanTheyUseThatTool )
